package com.controller;

import com.bean.PlanInfo;
import com.bean.QuestionInfo;
import com.bean.other.MetaInfo;
import com.bean.other.RequestData;
import com.bean.other.ResponseData;
import com.github.pagehelper.PageInfo;
import com.service.impl.PlanInfoService;
import com.service.impl.QuestionInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("api/ques_info")
public class QuestionInfoController {

    @Autowired
    private QuestionInfoService questionInfoService;

    @PostMapping("add")
    public ResponseEntity<ResponseData> add(@RequestBody QuestionInfo questionInfo){
        questionInfoService.insert(questionInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
        //return  ResponseEntity.status(HttpStatus.NOT_FOUND).body("没有找到");//直接设置响应头
    }

    @PostMapping("edit")
    public ResponseEntity<ResponseData> edit(@RequestBody QuestionInfo questionInfo){
        questionInfoService.update(questionInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
    }

    @DeleteMapping("del/{id}")
    public ResponseEntity<ResponseData> del(@PathVariable String id){
        QuestionInfo questionInfo=new QuestionInfo();
        questionInfo.setId(id);
        questionInfoService.delete(questionInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
    }

    @GetMapping("get")
    public ResponseEntity<ResponseData> get(@RequestParam("id")String id){
        QuestionInfo questionInfo=questionInfoService.get(id);
        return ResponseEntity.ok(ResponseData.success(questionInfo));
    }

    @PostMapping("list")//@RequestBody只能使用一次，使用的是流，用过后就不能用了。
    public ResponseEntity<ResponseData> list(@RequestBody RequestData<QuestionInfo> requestData){
        QuestionInfo questionInfo=requestData.getObj();
        questionInfo.setCreator(requestData.getUserid());
        MetaInfo metaInfo =requestData.getMetaInfo();
        PageInfo<QuestionInfo> pageInfo=questionInfoService.select(questionInfo,metaInfo);
        metaInfo.setPages(pageInfo.getPages());
        metaInfo.setTotal((int) pageInfo.getTotal());
        return ResponseEntity.ok(ResponseData.success(pageInfo.getList(),metaInfo));
    }

}
